package com.naughty.userlogin02.controller;

import com.alibaba.fastjson.JSON;
import com.naughty.userlogin02.bean.Book;
import com.naughty.userlogin02.bean.QueryInfo;
import com.naughty.userlogin02.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class BookController {

    @Autowired
    BookDao bookDao;

    @CrossOrigin
    @RequestMapping("/allBook")
    public String getBookList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = bookDao.getBookCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数，此处为模糊查询
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Book> book = bookDao.getAllBook("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",book);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @RequestMapping("/addBook")
    public String addBook(@RequestBody Book book){
        System.out.println(book);
        int i = bookDao.addBook(book);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/getUpdateBook")
    public String getUpdateBook(String bid){
        System.out.println("编号:"+bid);
        Book updatebooks = bookDao.getUpdateBook(bid);
        String users_json = JSON.toJSONString(updatebooks);
        return users_json;
    }

    @RequestMapping("/editBook")
    public String editBook(@RequestBody  Book book){
        System.out.println(book);
        int i = bookDao.editBook(book);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(@RequestBody  Book book){
        System.out.println(book.getBid());
        int i = bookDao.deleteBook(book.getBid());
        String str = i >0?"success":"error";
        return str;
    }


    @RequestMapping("/allBCBook")
    public String getBCBookList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = bookDao.getBookBCCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数，此处为模糊查询
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Book> book = bookDao.getAllBCBook("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",book);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @RequestMapping("/addBCBook")
    public String addBCBook(@RequestBody Book book){
        System.out.println(book);
        int i = bookDao.addBCBook(book);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/getUpdateBCBook")
    public String getUpdateBCBook(String bid){
        System.out.println("编号:"+bid);
        Book updatebooks = bookDao.getUpdateBCBook(bid);
        String users_json = JSON.toJSONString(updatebooks);
        return users_json;
    }

    @RequestMapping("/editBCBook")
    public String editBCBook(@RequestBody  Book book){
        System.out.println(book);
        int i = bookDao.editBCBook(book);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/deleteBCBook")
    public String deleteBCBook(String bid){
        System.out.println(bid);
        int i = bookDao.deleteBCBook(bid);
        String str = i >0?"success":"error";
        return str;
    }


    @RequestMapping("/allCBook")
    public String getCBookList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = bookDao.getBookCCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数，此处为模糊查询
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Book> book = bookDao.getAllCBook("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",book);
        System.out.println("总条数："+numbers);
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @RequestMapping("/addCBook")
    public String addCBook(@RequestBody Book book){
        System.out.println(book);
        int i = bookDao.addCBook(book);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/getUpdateCBook")
    public String getUpdateCBook(String bid){
        System.out.println("编号:"+bid);
        Book updatebooks = bookDao.getUpdateBCBook(bid);
        String users_json = JSON.toJSONString(updatebooks);
        return users_json;
    }

    @RequestMapping("/editCBook")
    public String editCBook(@RequestBody  Book book){
        System.out.println(book);
        int i = bookDao.editCBook(book);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/deleteCBook")
    public String deleteCBook(String bid){
        System.out.println(bid);
        int i = bookDao.deleteCBook(bid);
        String str = i >0?"success":"error";
        return str;
    }
}